#include "stringNode.h"

Node:: Node(){
	next= NULL;
	prev= NULL;
	element=0;
}

Node* Node:: getNext(){
	return next;
}

Node* Node:: getPrev(){
	return prev;
}

char Node::getElement(){
	return element;
}

void Node::setElement(char e){
	element=e;
}
//WORK
void Node::setNext(Node* n){
	next = n;
		n-> prev = this;
}
//WORK
void Node:: setPrev(Node* p){
	prev=p;
	p->next=this;
}

//HWORK
void Node:: showElement(){
	std::cout<<element;
}

Node::~Node(){
}

//WORK return string, character at tail not include
StringByNode StringByNode::substr(int first, int tail){

	StringByNode temp;

	Node* temp1=root;
	int i=0;
	while(temp1->getNext()&&i!=first){
		temp1=temp1->getNext();
		i++;
	}

	while(temp1->getNext()&& i!=tail){
		temp.push_back(temp1->getElement());
		temp1=temp1->getNext();
		i++;
	}

return temp;
}

//WORK
void StringByNode::deleteElement(int pos){
	if(pos>getLength()){
		return ;
	}
	else{
		Node* temp= root;
		int i=0;
		while(temp->getNext()&&i!=pos-1){
			temp=temp->getNext();
			i++;
		}
		Node * t1=temp;

		while(temp->getNext()&&i!=pos+1){
			temp=temp->getNext();
			i++;
		}

		Node* t2=temp;

		t1->setNext(t2);
		t2->setPrev(t1);
	}
}

//WORK
char StringByNode:: at(int pos){

	Node* temp=root;

	int i=0;
	while(temp->getNext()&&i!=pos){
		temp=temp->getNext();
		i++;
	}


	return temp->getElement();
}


//CHANGE LOGIC
int  StringByNode::find_first_of(char e, int pos){

	Node* temp1= root;
// to do logic
	while(temp1->getNext()&& temp1->getElement()!=e){
			temp1=temp1->getNext();
			pos++;
	}

	return pos;
}

//WORK
int  StringByNode:: getLength(){
	if(root){
		Node* temp=root;
		int count=0;
		while(temp->getNext()){
			temp=temp->getNext();
			count++;
		}
		return count;

	}
}

//WORK
void StringByNode::push_back(char e){

//	Node temp;
//	temp.setElement(e);

	if(root){
	Node* tem=root;

	while(tem->getNext()){

		tem=tem->getNext();
	}

	Node* nxt = new Node();

		tem -> setNext(nxt);
		tem -> getNext() ->setElement(e);

	}
	else{
		root= new Node();
		root->setElement(e);
	}

}

//WORK
void StringByNode:: addElement(int pos, char e){

	Node* temp=root;
	int i=0;
	while(temp->getNext()&&i!=pos){
		temp=temp->getNext();
		i++;
	}
	temp->setElement(e);

}

//WORK
bool StringByNode::isEquals(StringByNode str){

	if(this->getLength()!=str.getLength()){
		return false;
	}
	else{
		for(int i=0; i<getLength();i++)
			if(this->at(i)!=str.at(i))
				return false;
	}

	return true;
}

void StringByNode:: copy(StringByNode  str){

	Node* temp= str.getRoot();

	while(temp->getNext()){
		this->push_back(temp->getElement());
		temp=temp->getNext();

	}
	this->root= temp;
	//return *this;
}

/*
StringByNode StringByNode::operator =( const StringByNode &rhs){


return
}

*/

std::ostream& operator <<(std::ostream &os,  StringByNode str) {

	//size_t len = str.getLength();
	Node* temp= str.getRoot();

	while(temp->getNext()){
		os.put(temp->getElement());
		temp=temp->getNext();
	}

	return os;
}









